<?php 
header("Content-type: text/html; charset=utf-8"); 
include "TopSdk.php"; 
//将下载SDK解压后top里的TopClient.php第8行$gatewayUrl的值改为沙箱地址:http://gw.api.tbsandbox.com/router/rest, 
//正式环境时需要将该地址设置为:http://gw.api.taobao.com/router/rest 

//实例化TopClient类 
$c = new TopClient; 
$c->appkey = "21035140"; 
$c->secretKey = "c841262c692e2a38b18ae7568428b2e5"; 

//$req = new PosterChannelsGetRequest;
//执行API请求并打印结果 
//$resp = $c->execute($req); 
$db = mysql_connect("localhost", "tutorial", "86249317lijie");

mysql_select_db("grouplife",$db);

$array = array(2, 8);
for($i=0; $i<=2; $i++) {
	$reqPostList = new PosterPostersGetRequest;
	$reqPostList->setChannelId($array[$i]);
	$reqPostList->setPageSize(10);
	$reqPostList->setPageNo(1);
	$respPostList = $c->execute($reqPostList); 
	foreach($respPostList->posters->huabao as $t) {
		$reqPosterDetail = new PosterPosterdetailGetRequest;
		$reqPosterDetail->setPosterId((int)$t->id);
		$respPosterDetail = $c->execute($reqPosterDetail);

		$reqpostauction = new PosterPostauctionsGetRequest;
		$reqpostauction->setPosterId((int)$t->id);
		$resppostauction = $c->execute($reqpostauction);
		foreach($respPosterDetail->poster_pics->huabao_picture as $detail) {
			foreach($resppostauction->posterauctions->huabao_auction_info as $auction) {
				if((int)$detail->pic_id == (int)$auction->pic_id) {
					$reqitem = new ItemGetRequest;
					$reqitem->setFields("detail_url,num_iid,title,cid,nick,desc");
					$comm_id = trim($auction->auction_id);
					$reqitem->setNumIid($comm_id);
					$respitem = $c->execute($reqitem);
					$detail_url = $respitem->item->detail_url;
					$desc = $respitem->item->desc;
					$seller_nick = $respitem->item->nick;
					$cid = trim($respitem->item->cid);
					$category_id;
					$leimu_cid;
					$leimu_id;
					$leimu_name;

					$sqlgeturl = "SELECT * from stores where url = '$detail_url'";
					$urlresult = mysql_query($sqlgeturl, $db);
					$url_rows = mysql_num_rows($urlresult);
					if ($url_rows > 0) {
						print "Ingored: $detail_url\n";
					} else {
						$inserted ++;
						print "Inserted: $detail_url\n";
						$sqlcat = "SELECT id  from category where cid = $cid";
						$catresult = mysql_query($sqlcat, $db);
						$cat_rows = mysql_num_rows($catresult);
						if($cat_rows > 0) {
							$category_id = mysql_result($catresult,0,"id");
						} else {
							$reqcategory = new ItemcatsGetRequest;
							$reqcategory->setFields("cid,parent_cid,name,is_parent");
							$reqcategory->setCids($cid);
							$respcategory = $c->execute($reqcategory);
							$catname = trim($respcategory->item_cats->item_cat->name); 

							$parent_cid = trim($respcategory->item_cats->item_cat->parent_cid);
							if($parent_cid == 0) {
								$leimu_cid = $cid;
								$leimu_name = $catname;
							} else {
								while($parent_cid != '0') {
									$leimu_cid = $parent_cid;
									$parentreq = new ItemcatsGetRequest;
									$parentreq->setFields("cid,parent_cid,name,is_parent");
									$parentreq->setCids($leimu_cid);
									$parentresp = $c->execute($parentreq);
									$leimu_name = trim($parentresp->item_cats->item_cat->name);
									$parent_cid = trim($parentresp->item_cats->item_cat->parent_cid);
									echo "leimu_cid: $leimu_cid\n";
									echo "parent_cid: $parent_cid\n";
									echo "leimu name: $leimu_name\n";
								}
							}
							$catnames = explode("/", $catname);
							$catname = $catnames[0];
							$sqlinscat = "INSERT INTO category(name, cid, created, updated) values('$catname', $cid, current_timestamp, current_timestamp)";
							echo "$sqlinscat\n";
							$urlresult = mysql_query($sqlinscat, $db);
							$category_id = mysql_insert_id();

							$sqlleimu = "SELECT id  from leimu where cid = $leimu_cid";
							$leimuresult = mysql_query($sqlleimu, $db);
							$leimu_rows = mysql_num_rows($leimuresult);
							if($leimu_rows > 0) {
								$leimu_id = mysql_result($leimuresult,0,"id");
							} else {
								$sqlinsleimu = "INSERT INTO leimu(name, cid, created, updated) values('$leimu_name', $leimu_cid, current_timestamp, current_timestamp)";
								echo "$sqlinsleimu\n";
								$leimuinsresult = mysql_query($sqlinsleimu, $db);
								$leimu_id = mysql_insert_id();
							}
							$sqlleimucat = "INSERT INTO leimu_category(leimu_id, category_id, created, updated) values ($leimu_id, $category_id, current_timestamp, current_timestamp)";
							mysql_query($sqlleimucat);
						}
						

						$sqlowner = "SELECT id, username, email_address  from users where source_nick = '$seller_nick' and active = 'Y' and source = '1'";
						$ownerresult = mysql_query($sqlowner, $db);
						$owner_rows = mysql_num_rows($ownerresult);
						if ($owner_rows > 0) {
							print "User: $seller_nick was found for $detail_url\n";
							$owner_id = mysql_result($ownerresult,0,"id");
							$owner_username = mysql_result($ownerresult,0,"username");
							$owner_email = mysql_result($ownerresult,0,"email_address");
							$sqlcommitem = "INSERT INTO commented_item(comm_item_type_id, created, updated) values(1, current_timestamp, current_timestamp)";
							mysql_query($sqlcommitem);
							$comm_item_id = mysql_insert_id();
							$sqlstores = "INSERT INTO stores(name, bulletin, location, url, description, image, comm_item_id, created, updated, picked, owner_nick, owner_email) values('$auction->auction_title', '$detail->pic_note', 'shanghai', '$detail_url', '$desc', '$detail->pic_url', $comm_item_id, current_timestamp, current_timestamp, 'G', '$seller_nick', '$owner_email')";
							mysql_query($sqlstores);

							$store_id = mysql_insert_id();
							$sqlstorecat = "INSERT INTO stores_category(category_id, stores_id, created, updated) values ($category_id, $store_id, current_timestamp, current_timestamp)";
							mysql_query($sqlstorecat);
							$sqlstoresource = "INSERT INTO stores_source(source_id, stores_id, created, updated) values (1, $store_id, current_timestamp, current_timestamp)";
							mysql_query($sqlstoresource);

							$sqluserstores = "INSERT INTO user_stores(user_id, stores_id, created, updated, role) values($owner_id, $store_id, current_timestamp, current_timestamp, 'C')";
							mysql_query($sqluserstores);
						} else {
							$sqlcommitem = "INSERT INTO commented_item(comm_item_type_id, created, updated) values(1, current_timestamp, current_timestamp)";
							mysql_query($sqlcommitem);
							$comm_item_id = mysql_insert_id();
							$sqlstores = "INSERT INTO stores(name, bulletin, location, url, description, image, comm_item_id, created, updated, picked, owner_nick, owner_email) values('$auction->auction_title', '$detail->pic_note', 'shanghai', '$detail_url', '$desc', '$detail->pic_url', $comm_item_id, current_timestamp, current_timestamp, 'R', '$seller_nick', '')";
							mysql_query($sqlstores);
							$store_id = mysql_insert_id();
							$sqlstorecat = "INSERT INTO stores_category(category_id, stores_id, created, updated) values ($category_id, $store_id, current_timestamp, current_timestamp)";
							mysql_query($sqlstorecat);
							$sqlstoresource = "INSERT INTO stores_source(source_id, stores_id, created, updated) values (1, $store_id, current_timestamp, current_timestamp)";
							mysql_query($sqlstoresource);
						}
					}

				}
			}
		}
	}
}
?> 
